/*
 *@本JS功能：集群配置
 *@本JS作者：杨锡君
 *@编写时间：2018年06月21日
 */
$(function(){
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var colorArr = ['#4f87d9','#f16977','#6b76ac','#29b9a6'];
    function model(opt,num) {
        var str = '<div>' +
            '<div style="background: '+ colorArr[num] +'" class="oneTitle"><img src="pc/images/ico_imagesConfigTop.png" alt=""><span title="'+ opt.name +'" class="fl">'+ opt.name +'</span><span class="clear"></span><span class="fl userBox"><i>创建人：</i><i>'+ opt.username +'</i></span></div>' +
            '<ul class="oneContent">' +
            '<li title="'+ opt.masterImageName +'">主机镜像:'+ opt.masterImageName +'</li>' +
            '<li title="'+ opt.firstSlaveImageName +'">从机1镜像:'+ opt.firstSlaveImageName +'</li>' +
            '<li title="'+ opt.secondSlaveImageName +'">从机1镜像:'+ opt.secondSlaveImageName +'</li>' +
            '</ul>' +
            '<div class="oneBottom"><img data-id="'+ opt.id +'"  src="pc/images/ico_imagesConfigDel.png" title="删除" class="delBtn fr"><img title="设置" src="pc/images/ico_imagesConfigSet.png" data-id="'+ opt.id +'" class="setBtn fr"></div>' +
            '</div>';
        return str;
    }
    var addModel = "<div class=\"popup\">"
        +"<div class=\"popup_box\">"
        +"<div class=\"popup_contenthead imName\">创建集群配置<img class=\"popup_img\" src=\"pc/images/ico_alertDel.png\"></div>"
        +"<div class=\"popup_content\" style=\"padding: 20px 100px 5px;\">"
        +"<div>&ensp;&ensp;集群名称:&ensp;<input class='publicText' id='imageName'maxlength='20'  placeholder='请输入集群名称' autocomplete=\"off\" type=\"text\" /></div>"
        +"<div>&ensp;&ensp;&emsp;主节点:&ensp;<select name='' class='publicSelect' id='master'></select></div>"
        +"<div>&ensp;&ensp;&emsp;从节点:&ensp;<select name='' class='publicSelect' id='slave'></select></div>"
        +"<div>&ensp;&ensp;&emsp;从节点:&ensp;<select name='' class='publicSelect' id='slaves'></select></div>"
        +"</div>"
        +"<div class=\"popup_btn\">"
        +"<a class=\"popup_confirm exportBtn hover-btn\">确定</a>"
        +"<a class=\"popup_close exportCancle hover-btn\">取消</a>"
        +"</div>"
        +"<div class=\"clear\"></div>"
        +"</div>"
        +"</div>";
    var editModel = "<div class=\"popup\">"
        +"<div class=\"popup_box\">"
        +"<div class=\"popup_contenthead imName\">修改集群配置<img class=\"popup_img\" src=\"pc/images/ico_alertDel.png\"></div>"
        +"<div class=\"popup_content\" style=\"padding: 20px 100px 5px;\">"
        +"<div>&ensp;&ensp;集群名称:&ensp;<input class='publicText' id='imageName' maxlength='20' placeholder='请输入集群名称' autocomplete=\"off\" type=\"text\" /></div>"
        +"<div>&ensp;&ensp;&emsp;主节点:&ensp;<select name='' class='publicSelect' id='master'></select></div>"
        +"<div>&ensp;&ensp;&emsp;从节点:&ensp;<select name='' class='publicSelect' id='slave'></select></div>"
        +"<div>&ensp;&ensp;&emsp;从节点:&ensp;<select name='' class='publicSelect' id='slaves'></select></div>"
        +"</div>"
        +"<div class=\"popup_btn\">"
        +"<a class=\"popup_confirm exportBtn hover-btn\">确定</a>"
        +"<a class=\"popup_close exportCancle hover-btn\">取消</a>"
        +"</div>"
        +"<div class=\"clear\"></div>"
        +"</div>"
        +"</div>";
    var imageConfig = {
        str:"",
        init:function(){
            this.bindEvent();
        },
        bindEvent:function(){
            var self = this;
            self.getImages();
            self.getDataList();
            self.addImage();
        },
        getImages:function(){
            var self = this;
            $.ajax({
                type:'get',
                url:"manage/image/list",
                async:true,
                success:function (msg) {
                    if(msg.code == 200) {
                        var str = "";
                        $.map(msg.data,function (opt) {
                            str += "<option value='"+ opt.id +"'>"+ opt.name +"</option>";
                        });
                        self.str = str;
                    } else {
                        Alert1.content = msg.message;
                        Alert1.init();
                        Alert1.show();
                    }
                }
            });
        },
        delImage:function(){
            $(".delBtn").off("click").click(function () {
                var json = {"id":Number($(this).attr("data-id")),"isForced":false};
                confirm1.content = "确定删除当前集群镜像吗？";
                confirm1.success = function () {
                    loading1.show();
                    $.ajax({
                        type:'get',
                        url:"manage/cluster/delete",
                        async:true,
                        data:json,
                        success:function (msg) {
                            loading1.hide();
                            if(msg.code == 200) {
                                Alert1.content = "删除成功";
                                Alert1.success = function () {
                                    window.location.reload(true);
                                };
                                Alert1.init();
                                Alert1.show();
                            } else if(msg.code == 500305){
                                confirm1.content = msg.message;
                                confirm1.success = function () {
                                    json.isForced = true;
                                    $.ajax({
                                        type: 'get',
                                        url: "manage/cluster/delete",
                                        async: true,
                                        data: json,
                                        success: function (msg) {
                                            loading1.hide();
                                            if (msg.code == 200) {
                                                Alert1.content = "删除成功";
                                                Alert1.success = function () {
                                                    window.location.reload(true);
                                                };
                                                Alert1.init();
                                                Alert1.show();
                                            }else{
                                                Alert1.content = msg.message;
                                                Alert1.init();
                                                Alert1.show();
                                            }
                                        }
                                    });
                                };
                                confirm1.init();
                                confirm1.show();

                            }else{
                                Alert1.content = msg.message;
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });
                };
                confirm1.init();
                confirm1.show();
            });
        },
        setImage:function () {
            var self = this;
            $(".setBtn").off("click").click(function () {
                self.popEvent(2,Number($(this).attr("data-id")));
            });
        },
        addImage:function () {
            var self = this;
            $(".configAdd").off("click").click(function () {
                $.ajax({
                    type:'get',
                    url:"manage/image/list",
                    async:true,
                    success:function (msg) {
                        if(msg.code == 200) {
                            if(msg.data.length!=0){
                                self.popEvent(1);
                            }else{
                                Alert1.content = "当前系统没有镜像";
                                Alert1.init();
                                Alert1.show();
                            }
                        } else {
                            Alert1.content = msg.message;
                            Alert1.init();
                            Alert1.show();
                        }
                    }
                });

            });
        },
        popEvent:function(mark,id){
            self = this;
            $(".masking").show();
            if(mark == 2){
                $("body").append(editModel);
            }else{
                $("body").append(addModel);
            }
            maxLengthLimit.init("publicText");
            $("#master").append(self.str);
            $("#slave").append(self.str);
            $("#slaves").append(self.str);
            if(mark == 2){
                var jsons = {"id":id};
                $.ajax({
                    type:'get',
                    url:"manage/cluster/query",
                    async:true,
                    data:jsons,
                    success:function (msg) {
                        if(msg.code == 200) {
                            $("#imageName").val(msg.data.name);
                            $("#master").val(msg.data.masterImageId);
                            $("#slave").val(msg.data.firstSlaveImageId);
                            $("#slaves").val(msg.data.secondSlaveImageId);
                        } else {
                            Alert1.content = msg.message;
                            Alert1.init();
                            Alert1.show();
                        }
                    }
                });
            }
            var url = "";
            $(".exportBtn").off("click").click(function () {
                var json = {"name":$("#imageName").val(),"masterImageId":Number($("#master").val())==0?'':Number($("#master").val()),"firstSlaveImageId":Number($("#slave").val())==0?'':Number($("#slave").val()),"secondSlaveImageId":Number($("#slaves").val())==0?'':Number($("#slaves").val())};
                if(mark == 1){
                    url = "manage/cluster/create";
                }else{
                    url = "manage/cluster/update";
                    json.id = Number(id);
                }
                json = JSON.stringify(json);
                if($("#imageName").val() == ""){
                    Alert1.content = "请输入集群名称";
                    Alert1.init();
                    Alert1.show();
                }else{
                    loading1.show();
                    $.ajax({
                        type:'post',
                        url:url,
                        async:true,
                        dataType:'JSON',
                        contentType:'application/json',
                        data:json,
                        success:function (msg) {
                            loading1.hide();
                            if(msg.code == 200) {
                                Alert1.content = "保存成功";
                                Alert1.success = function () {
                                    window.location.reload(true);
                                };
                                Alert1.init();
                                Alert1.show();
                            } else {
                                Alert1.content = msg.message;
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });
                }

            });
            $(".exportCancle").off("click").click(function () {
                $(".masking").hide();
                $(".popup").remove();
            });
            $(".popup_img").off("click").click(function () {
                $(".masking").hide();
                $(".popup").remove();
            });
        },
        getDataList:function () {
            var self = this;
            $.ajax({
                type:'get',
                url:'manage/cluster/list',
                async:true,
                success:function (msg) {
                    if(msg.code == 200) {
                        var str = "";
                        var num =0;
                        $.map(msg.data,function (opt) {
                            str += model(opt,num);
                            num++;
                            if(num == 4){
                                num = 0;
                            }
                        });
                        $(".addBox").before(str);
                        self.delImage();
                        self.setImage();
                        $(".delBtn").hover(function () {
                            $(this).attr("src","pc/images/ico_imagesConfigDel2.png");
                        },function () {
                            $(this).attr("src","pc/images/ico_imagesConfigDel.png");
                        });
                        $(".setBtn").hover(function () {
                            $(this).attr("src","pc/images/ico_imagesConfigSet2.png");
                        },function () {
                            $(this).attr("src","pc/images/ico_imagesConfigSet.png");
                        });
                    } else {
                        Alert1.content = msg.message;
                        Alert1.init();
                        Alert1.show();
                        return false;
                    }
                }
            });
        }

    };
    imageConfig.init();
});
